{% extends "pretixcontrol/items/base.html" %}
{% load i18n %}
{% load urlreplace %}
{% block title %}{% trans "Quotas" %}{% endblock %}
{% block inside %}
    <nav id="event-nav" class="header-nav">
        <div class="navigation">
            <div class="navigation-title">
                <h1>{% trans "Quotas" %}</h1>
            </div>
            {% include "pretixcontrol/event/component_link.html" %}
        </div>
    </nav>
    <p>
        {% blocktrans trimmed %}
        To ensure your products are available as intended, you need to set quotas. Quotas determine how many instances of your product can be sold on the platform. This allows you to configure whether your event can accommodate an unlimited number of attendees or if the number of attendees is capped. You can assign a product to multiple quotas to meet more complex requirements, such as limiting the total number of tickets sold while also restricting the number of specific ticket types simultaneously.
        {% endblocktrans %}
    </p>
    {% if request.event.has_subevents %}
        <form class="form-inline helper-display-inline" action="" method="get">
            {% include "pretixcontrol/event/fragment_subevent_choice_simple.html" %}
        </form>
    {% endif %}
    {% if quotas|length == 0 %}
        <div class="empty-collection">
            <p>
                {% if request.GET.subevent %}
                    {% trans "Your search did not match any quotas." %}
                {% else %}
                    {% blocktrans trimmed %}
                        You haven't created any quotas yet.
                    {% endblocktrans %}
                {% endif %}
            </p>

            <a href="{% url "control:event.products.quotas.add" organizer=request.event.organizer.slug event=request.event.slug %}"
                    class="btn btn-primary btn-lg"><i class="fa fa-plus"></i> {% trans "Create a new quota" %}</a>
        </div>
    {% else %}
        <p>
            <a href="{% url "control:event.products.quotas.add" organizer=request.event.organizer.slug event=request.event.slug %}" class="btn btn-default"><i class="fa fa-plus"></i> {% trans "Create a new quota" %}
            </a>
        </p>
        <div class="table-responsive">
            <table class="table table-hover table-quotas">
                <thead>
                <tr>
                    <th>{% trans "Quota name" %}
                        <a href="?{% url_replace request 'ordering' '-name' %}"><i class="fa fa-caret-down"></i></a>
                        <a href="?{% url_replace request 'ordering' 'name' %}"><i class="fa fa-caret-up"></i></a></th>
                    </th>
                    <th>{% trans "Products" %}</th>
                    {% if request.event.has_subevents %}
                        <th>{% trans "Date" context "subevent" %}
                            <a href="?{% url_replace request 'ordering' '-date' %}"><i class="fa fa-caret-down"></i></a>
                            <a href="?{% url_replace request 'ordering' 'date' %}"><i class="fa fa-caret-up"></i></a></th>
                        </th>
                    {% endif %}
                    <th>{% trans "Total capacity" %}
                        <a href="?{% url_replace request 'ordering' '-size' %}"><i class="fa fa-caret-down"></i></a>
                        <a href="?{% url_replace request 'ordering' 'size' %}"><i class="fa fa-caret-up"></i></a></th>
                    </th>
                    <th>{% trans "Capacity left" %}</th>
                    <th class="action-col-2"></th>
                </tr>
                </thead>
                <tbody>
                {% for q in quotas %}
                    <tr>
                        <td>
                            <strong><a href="{% url "control:event.products.quotas.show" organizer=request.event.organizer.slug event=request.event.slug quota=q.id %}">{{ q.name }}</a></strong>
                        </td>
                        <td>
                            <ul>
                                {% for product in q.cached_products %}
                                    {% if not product.has_variations %}
                                        <li><a href="{% url "control:event.product" organizer=request.event.organizer.slug event=request.event.slug product=product.id %}">{{ product }}</a></li>
                                    {% endif %}
                                {% endfor %}
                                {% for v in q.variations.all %}
                                    <li><a href="{% url "control:event.product" organizer=request.event.organizer.slug event=request.event.slug product=v.product.id %}#tab-0-3-open">
                                        {{ v.product }} – {{ v }}</a></li>
                                {% endfor %}
                            </ul>
                        </td>
                        {% if request.event.has_subevents %}
                            <td>
                                {{ q.subevent.name }} – {{ q.subevent.get_date_range_display }} {{ q.subevent.date_from|date:"TIME_FORMAT" }}
                            </td>
                        {% endif %}
                        <td>{% if q.size == None %}Unlimited{% else %}{{ q.size }}{% endif %}</td>
                        <td>{% include "pretixcontrol/items/fragment_quota_availability.html" with availability=q.cached_avail closed=q.closed %}</td>
                        <td class="text-right flip">
                            <a href="{% url "control:event.products.quotas.edit" organizer=request.event.organizer.slug event=request.event.slug quota=q.id %}" class="btn btn-default btn-sm"><i class="fa fa-edit"></i></a>
                            <a href="{% url "control:event.products.quotas.add" organizer=request.event.organizer.slug event=request.event.slug %}?copy_from={{ q.id }}"
                               class="btn btn-sm btn-default" title="{% trans "Clone" %}" data-toggle="tooltip">
                                <span class="fa fa-copy"></span>
                            </a>
                            <a href="{% url "control:event.products.quotas.delete" organizer=request.event.organizer.slug event=request.event.slug quota=q.id %}" class="btn btn-delete btn-danger btn-sm"><i class="fa fa-trash"></i></a>
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    {% endif %}
    {% include "pretixcontrol/pagination.html" %}
{% endblock %}
